//
//  JSLFeedBackController.m
//  Friendsback
//
//  Created by 宋质彬 on 2018/3/7.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JSLFeedBackController.h"

#import "JSLTextView.h"
@interface JSLFeedBackController ()<UITextViewDelegate>
@property (nonatomic , strong) UIView *backView;
@property (nonatomic , weak) JSLTextView *textView;
@property (nonatomic , weak) UILabel *textLengthLab;

@end

@implementation JSLFeedBackController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



#pragma mark - View创建与设置

- (void)setUpView{
    
    [self baseConfigBackTitle:PageGobackTypePop];
   
    self.edgesForExtendedLayout = UIRectEdgeNone;
    _backView = [JSLKitFactory jsl_viewWithFrame:CGRectMake(10*AutoSizeScaleXX, 10*AutoSizeScaleYY, self.view.width -10*AutoSizeScaleXX*2, 180*AutoSizeScaleYY) backgroundColor:WHITE_COLOR superView:self.view];
    
    JSLTextView *textView = [[JSLTextView alloc] init];
    textView.frame = CGRectMake(25*AutoSizeScaleXX, 10*AutoSizeScaleYY, self.view.width -25*AutoSizeScaleYY *2, _backView.height);
    textView.text = nil;
    [textView becomeFirstResponder] ;
    // 设置文本字体
    textView.font = REGULAR_FONT(16);
    // 设置文本颜色
    textView.textColor = BACK_COLOR;
    // 设置文本框背景颜色
    textView.backgroundColor = [UIColor clearColor];
    // 设置文本对齐方式
    textView.textAlignment = NSTextAlignmentLeft;
    // 设置自动纠错方式
    textView.autocorrectionType = UITextAutocorrectionTypeNo;
    
    textView.showsVerticalScrollIndicator = NO;
    // 设置自动大写方式
    textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    textView.keyboardType = UIKeyboardTypeDefault;
    
    // 设置return键样式
    textView.returnKeyType = UIReturnKeyDefault;
    
    // 设置是否可以拖动
    textView.scrollEnabled = YES;
    // 设置代理
    textView.delegate = self;
    //自适应高度
    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    // 自定义文本框字数统计
    textView.placeholder = @"您的声音，我们倾听";
    textView.placeholderColor = [@"#999999" hexStringToColor];
    textView.placeholderFont = REGULAR_FONT(15);
    [_backView addSubview:textView];
    self.textView = textView;
    

    
    self.textLengthLab = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:@"0/200" font:LIGHT_FONT(12) textColor:[@"#CCCCCC" hexStringToColor] textAlignment:NSTextAlignmentRight SuperView:_backView];
    
    CGSize textSize = [self.textLengthLab.text sizeWithAttributes:@{NSFontAttributeName : self.textLengthLab.font}];
    self.textLengthLab.frame = CGRectMake(_backView.width - 10*AutoSizeScaleXX - textSize.width, _backView.height - 10*AutoSizeScaleYY - textSize.height, textSize.width, textSize.height);
    
    
    UIButton * btnSend = [JSLKitFactory jsl_titleButtonWithFrame:CGRectMake(10*AutoSizeScaleXX, _backView.bottom+37*AutoSizeScaleYY, self.view.width -10*AutoSizeScaleXX*2, 50*AutoSizeScaleYY) superView:self.view font:REGULAR_FONT(16) titleColor:WHITE_COLOR backgroundColor:[@"#2BA6FF" hexStringToColor] title:@"提交" target:self action:@selector(commitAction:)];
    
    btnSend.layer.mask = [JSLKitFactory roundedRect:btnSend.bounds roundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(2, 2)];
    
   
    
    
    
}



#pragma mark - 初始化数据源



#pragma mark - 按钮事件

-(void)commitAction:(UIButton *)sender{
    
    if ([JSLKitFactory isNotNull:self.textView.text]) {
        NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
        parameters[@"feedBackContent"] = self.textView.text;
        [HttpRequest POST:LOAN_FEEDBAXK parameters:parameters success:^(id  _Nullable result, NSString * _Nullable successMessage) {
            
            // 跳转
            NSLog(@"%@",result);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showMessage:@"提交成功"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self popViewController];
                });
                
            });
            
            
        } failure:^(NSInteger code, NSString * _Nullable errorMessage) {
            NSLog(@"%@",errorMessage);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showMessage:@"提交失败"];
            });
            
        }];
    }

    
   
}



#pragma mark - Delegate

- (void)textViewDidChange:(UITextView *)textView
{
    if ([textView.text trimmingWhitespaceAndNewlines].length==0){
     
       
//        [_tvResponse becomeFirstResponder];
    }else{
  
    }
    //该判断用于联想输入
    if (textView.text.length > 200)
    {
        //        _lblAmount.text = @"0";
        textView.text = [textView.text substringToIndex:200];
    }else {
        self.textLengthLab.text = [NSString stringWithFormat:@"%lu/200",(unsigned long)textView.text.length];
        CGSize textSize = [self.textLengthLab.text sizeWithAttributes:@{NSFontAttributeName : self.textLengthLab.font}];
        self.textLengthLab.frame = CGRectMake(_backView.width - 10*AutoSizeScaleXX - textSize.width, _backView.height - 10*AutoSizeScaleYY - textSize.height, textSize.width, textSize.height);
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
